package com.github.data_manage.dao

import com.github.data_manage.domain.po.ContrastBusinessData
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import java.math.BigDecimal
import java.time.LocalDate

/**
 *@author CHN
 *@since 2024-08-10 14:48
 */
interface ContrastBusinessDataDao : BaseDao<ContrastBusinessData> {

    fun findContrastBusinessDataByContrastIdAndTime(contrastId: Int, time: LocalDate): ContrastBusinessData?

    @Query("select max(f.value) from ContrastBusinessData f where f.contrastId = :contrastId and f.time <= :time")
    fun findMaxValueByContrastIdAndTime(@Param("contrastId")contrastId: Int,@Param("time") time: LocalDate): BigDecimal?

    fun deleteAllByContrastId(contrastId: Int)

    @Query("select f from ContrastBusinessData f where f.contrastId = :contrastId order by f.time")
    fun findAllByContrastId(contrastId: Int): List<ContrastBusinessData>

    fun findAllByContrastIdAndTimeBetween(contrastId: Int, startTime: LocalDate, endTime: LocalDate): List<ContrastBusinessData>

    fun findAllByTime(time: LocalDate): List<ContrastBusinessData>

    @Query("select f from ContrastBusinessData f where f.contrastId = :contrastId order by f.time desc limit 12")
    fun findLast12Values(contrastId: Int): List<ContrastBusinessData>

    fun findAllByTimeBetweenAndContrastIdIn(startTime: LocalDate, endTime: LocalDate, contrastIds: List<Int>): List<ContrastBusinessData>
}
